package gui;

import database.*;

import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.text.DecimalFormat;
import java.util.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class PanelKvittering extends JPanel implements ActionListener{
	private JTextArea textArea;
	private JScrollPane scrollPane;
	private JButton buttonOK;
	private JLabel labelKvittering;
	private GridBagConstraints constraints;
	private Bestilling bestilling;
	private Kunde kunde;
	public PanelKvittering(Bestilling bestilling){
		this.kunde = Register.findKundeObjekt(bestilling.getKundeID());
		this.bestilling = bestilling;
		this.setLayout(new GridBagLayout());
		this.leggTilKomponenter();
		GUI.getVindu().add(this);
	}
	private void leggTilKomponenter(){
		textArea = new JTextArea(kvitteringsStreng());
		Font font = new Font(textArea.getFont().getName(), textArea.getFont().getStyle(), 16);
		textArea.setFont(font);
		scrollPane = new JScrollPane(textArea);
		
		buttonOK = new JButton("OK");
		buttonOK.addActionListener(this);
		
		labelKvittering = new JLabel("Kvittering");
		font = new Font(labelKvittering.getFont().getName(), labelKvittering.getFont().getStyle(), 30);
		labelKvittering.setFont(font);
		
		constraints = new GridBagConstraints();
		constraints.insets = new Insets(10, 10, 10, 10);
		constraints.gridy = 0;
		this.add(labelKvittering, constraints);
		constraints.gridy = 1;
		this.add(scrollPane,constraints);
		constraints.gridy = 2;
		constraints.anchor = GridBagConstraints.SOUTH;
		this.add(buttonOK,constraints);
	}
	private String kvitteringsStreng(){
		DecimalFormat toDesi;
		toDesi = new DecimalFormat("0.00");
		String utskrift = ""; 
		if(kunde==null)
			utskrift = "kunde er null";
		else if(kunde!=null){
			utskrift += "Kunde: " + kunde.getfNavn() + " " + kunde.geteNavn()+"\n";
			utskrift += "Adresse: " + kunde.getAdresse() + "\n" + kunde.getPostNr() + " " + Register.getPostSted(kunde.getPostNr()) + "\n" + "Telefonnummer: " +  kunde.getTlf() + "\n\n";
			List<Rett> retter = Register.findRetter(bestilling);
			for (int j = 0; j < retter.size(); j++) {
				utskrift += "Rett: " + retter.get(j).getNavn() + "\n";
			}
			utskrift += "Leveringstidspunkt: " + bestilling.getLeveringstidspunkt() +"\n" + "Leveringsadresse: " + bestilling.getLeveringsadresse() + "\n" + bestilling.getPostNr() + " " + Register.getPostSted(bestilling.getPostNr()) + "\n";
			utskrift += "Annet: " + bestilling.getAnnet() + "\n" + "Pris uten moms: " + toDesi.format(bestilling.getBellop()) + "\n";
			utskrift += "Pris med moms: " + toDesi.format(Register.beregnPrisTaMed(bestilling)) + "\n" + "Fraktpris: " + toDesi.format(fraktpris()) + "\n" + "Totalpris: " + toDesi.format(totalpris()) + "\n"; ;
		}
			
			
		return utskrift;
	}
	private void buttonOK_actionPerformed(ActionEvent e)
	{
		this.setVisible(false);
		new PanelHovedmeny();
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==buttonOK)
			buttonOK_actionPerformed(e);
		
	}
	private double fraktpris(){
		if(bestilling.getLeveringsadresse().equals("Hentes")){
			return 0;
		}
		else if(bestilling.getBellop()<Databasecon.getGratisLeveringBellop()){
			return Databasecon.getFraktPris();
		}
		else{
			return 0;
		}
	}
	private double totalpris(){
		return Register.beregnPrisTaMed(bestilling) + fraktpris();
	}

}
